package com.maiji.cloud.microservice;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Maps;
import com.maiji.cloud.entities.login.UploadRecord;
import com.maiji.cloud.response.login.UploadImageResData;
import com.maiji.cloud.service.UploadRecordService;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/uploadRecord/")
public class UploadRecordController {

    @Autowired
    private UploadRecordService uploadRecordService;

    /**
     *  修改图片上传记录
     * @param uploadRecord
     * @return
     */
    @PostMapping("updateUploadRecord")
    public Boolean updateUploadRecord (@RequestBody UploadRecord uploadRecord) {
        if (uploadRecord == null) return false;
        return uploadRecordService.updateById(uploadRecord);
    }

    /**
     *  根据belongId重置图片归属
     * @param belongId
     * @return
     */
    @PostMapping("resetUploadRecord")
    public Boolean resetUploadRecord (@RequestBody String belongId, @RequestHeader(name = "isMain") Integer isMain) {
        if (StringUtil.isBlank(belongId)) return false;
        EntityWrapper<UploadRecord> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("belong_id", belongId);
        entityWrapper.eq("is_main", isMain);
        return uploadRecordService.updateForSet("belong_id = null", entityWrapper);
    }

    /**
     *  批量修改图片上传记录
     * @param uploadRecords
     * @return
     */
    @PostMapping("updateBatchUploadRecord")
    public Boolean updateBatchUploadRecord (@RequestBody List<UploadRecord> uploadRecords) {
        if (uploadRecords == null || uploadRecords.size() == 0) return false;
        return uploadRecordService.updateBatchById(uploadRecords);
    }

    /**
     *  批量保存图片上传记录
     * @param uploadRecords
     * @return
     */
    @PostMapping("insertBatchUploadRecord")
    public Boolean insertBatchUploadRecord (@RequestBody List<UploadRecord> uploadRecords) {
        return uploadRecordService.insertBatch(uploadRecords);
    }

    /**
     *  根据belongId批量删除图片上传记录
     * @param belongId
     * @return
     */
    @PostMapping("delBatchByBelongId")
    public Boolean delBatchByBelongId (@RequestBody String belongId) {
        EntityWrapper<UploadRecord> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("belong_id", belongId);
        return uploadRecordService.delete(entityWrapper);
    }

    /**
     *  根据 belongId[] 批量获取图片
     * @param map
     * @return
     */
    @PostMapping("findAllByBelongIds")
    public List<UploadRecord> findAllByBelongIds (@RequestBody Map<String, Object> map) {
        Collection<String> belongIds = (Collection<String>) map.get("belongIds");
        Integer isMain = (Integer) map.get("isMain");
        if (belongIds != null && belongIds.size() == 0) return null;
        EntityWrapper<UploadRecord> entityWrapper = new EntityWrapper<>();
        entityWrapper.in("belong_id", belongIds);
        if (isMain != null) entityWrapper.eq("is_main", isMain);
        Integer no = (Integer) map.get("no");
        if (no != null) entityWrapper.eq("no", no);
        String orderBy = (String) map.get("orderBy");
        Boolean isAsc = (Boolean) map.get("isAsc");
        if (StringUtil.isNotBlank(orderBy) && isAsc != null) entityWrapper.orderBy(orderBy, isAsc);
        return uploadRecordService.selectList(entityWrapper);
    }

    /**
     *  根据 belongId[] 批量获取图片
     * @param belongIds
     * @return
     */
    @PostMapping("findAllByBelongIds0")
    public List<UploadRecord> findAllByBelongIds (@RequestBody List<String> belongIds) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("belongIds", belongIds);
        return findAllByBelongIds(map);
    }

    /**
     *  根据 belongId[] 批量获取图片
     * @param map
     * @return
     */
    @PostMapping("findResDataList")
    public List<UploadImageResData> findResDataList (@RequestBody Map<String, Object> map) {
        List<UploadRecord> uploadRecords = findAllByBelongIds(map);
        return uploadRecords.parallelStream().map(uploadRecord -> {
            UploadImageResData uploadImageResData = new UploadImageResData();
            BeanUtils.copyProperties(uploadRecord, uploadImageResData);
            return uploadImageResData.setSmallUrl(uploadRecord.getUrl() + "?x-oss-process=image/resize,l_500");
        }).collect(Collectors.toList());
    }

}
